package com.puman.beautyfarmcounselor.ui.task.adpter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.ui.task.MemberFragment_;
import com.puman.beautyfarmcounselor.ui.task.TaskFragment;
import com.puman.beautyfarmcounselor.ui.task.model.TaskItemList;

/**
 * Created by Curry on 2015/6/12.
 */
public class TaskListViewAdapter extends RecyclerView.Adapter<TaskListViewAdapter.ViewHolder> {

    private  TaskFragment taskFragment;
    private  TaskItemList taskItemList ;
    private int mImageViewArray[] = new int[4];

    public TaskListViewAdapter(TaskFragment taskFragment, TaskItemList taskItemList, int[] mImageViewArray) {
        this.taskFragment = taskFragment;
        this.taskItemList = taskItemList;
        this.mImageViewArray = mImageViewArray;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView title;
        private final TextView proportion;
        private final TextView tipDetail;
        private final ImageView icon;
        private final ProgressBar progress;
        private final LinearLayout tipLayout;
        private final LinearLayout taskItemLayout;

        public ViewHolder(View v) {
            super(v);
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("curry", "onclick " + getPosition());
                }
            });
            title = (TextView) v.findViewById(R.id.task_list_item_name);
            proportion = (TextView) v.findViewById(R.id.task_list_proportion);
            tipDetail = (TextView) v.findViewById(R.id.task_list_tip);
            icon = (ImageView) v.findViewById(R.id.task_list_item_icon);
            progress = (ProgressBar) v.findViewById(R.id.task_list_process_bar);
            tipLayout = (LinearLayout) v.findViewById(R.id.task_list_tip_layout);
            taskItemLayout = (LinearLayout) v.findViewById(R.id.task_list_item_layout);
        }

        public TextView getTitle() {
            return this.title;
        }

        public TextView getProportion() {
            return this.proportion;
        }

        public TextView getTipDetail() {
            return this.tipDetail;
        }

        public ImageView getIcon() {
            return this.icon;
        }

        public ProgressBar getProgress() {
            return this.progress;
        }

        public LinearLayout getTipLayout() {
            return tipLayout;
        }

        public LinearLayout getTaskItemLayout() {
            return taskItemLayout;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.task_list_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (taskItemList.getData().get(position).getDescription().length() == 0) {
            holder.getTipLayout().setVisibility(View.GONE);
        } else {
            holder.getTipDetail().setText(taskItemList.getData().get(position).getDescription());
            holder.getTipLayout().setVisibility(View.VISIBLE);
        }

        holder.getIcon().setImageResource(mImageViewArray[position % taskItemList.getData().size()]);
        int finishCount = taskItemList.getData().get(position).getFinish_count();
        int unfinishCount = taskItemList.getData().get(position).getUnfinish_count();

        if (taskItemList.getData().get(position).getUnfinish_count() != 0){
            holder.getProgress().setProgress((finishCount *100) / unfinishCount);
        }else{
            holder.getProgress().setProgress(0);
        }

        holder.getProportion().setText(finishCount + "/"+ unfinishCount);
        holder.getTitle().setText(taskItemList.getData().get(position).getName());
        holder.getTaskItemLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(MainActivity.getInstace(), "******" + position, Toast.LENGTH_SHORT).show();
                MainActivity.getInstace().switchFragment(taskFragment, MemberFragment_.builder().taskItem(taskItemList.getData().get(position)).build());
//                MainActivity.getInstace().switchFragment(taskFragment, MemberFragment_.builder().titleStr(taskItemList.getData().get(position).getName())
//                        .campainCode(taskItemList.getData().get(position).getCode()).build());
//                MainActivity.getInstace().getSupportFragmentManager()
//                        .beginTransaction().hide(taskFragment).replace(R.id.content_none_bottom, MemberFragment_.builder().titleStr(taskItemList.getData().get(position).getName()).build()).addToBackStack(null).commit();
            }
        });
    }

    @Override
    public int getItemCount() {
        return taskItemList.getData().size();
    }

}
